<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>2. 排序</title>
</head>

<body>
  <p>请打开控制台查看输出。</p>

  <script>
    let a = [{ name: 'apple', issold: false }, { name: 'orange', issold: false }, { name: 'banana', issold: true }, { name: 'pear', issold: true }]
    // 请按照已售，未售排序，同时在同一已售未售下按照名称升序排列

    function sortName(a, b) {
      return a.name - b.name
    }

    function sortArr(arr) {
      let sold = [], notSold = []

      a.forEach(i => {
        if (i.issold) {
          sold.push(i)
        } else {
          notSold.push(i)
        }
      })

      sold.sort(sortName)
      notSold.sort(sortName)

      return sold.concat(notSold)
    }

    console.log(sortArr(a))
  </script>
</body>

</html>